/***************************************************************************
 *   This file is part of KTagger                                          *
 *   Copyright (C) 2010 by Luca Bellonda                                   *
 *   lbellonda _at_ gmail.com                                              *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "TagChooser.h"
#include "ui_TagChooser.h"
#include <QTreeWidgetItem>
#include <QPushButton>
#include <QDialogButtonBox>
#include "Utility.h"


TagProfile* chooseProfile(QWidget *parent, ktagger::KTaggerApplication *application)
{
    TagChooser tagChooser(application, parent);
    tagChooser.setModal(true);
    if (tagChooser.exec() == QDialog::Accepted) {
        return tagChooser.currentProfile();
    }
    return NULL ;
}

TagChooser::TagChooser(ktagger::KTaggerApplication *application, QWidget *parent) :
        QDialog(parent),
        ui(new Ui::Dialog)
{
    _selectedProfile = NULL;
    _application = application ;
    _profiles = NULL ;
    ui->setupUi(this);
    ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
    QTreeWidget *pTree = ui->treeWidget ;
    pTree->setRootIsDecorated(false);
    pTree->setColumnCount(3);
    QStringList headers;
    headers << tr("Name");
    headers << tr("Description");
    headers << tr("Tags");
    pTree->setHeaderLabels(headers);

    setupData();
}

TagChooser::~TagChooser()
{
    deleteData();
    delete ui;
}

void TagChooser::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

TagProfile* TagChooser::currentProfile()
{
    return _selectedProfile ;
}

void TagChooser::setCurrent(QTreeWidgetItem * item)
{
    QVariant data = item->data(0, Qt::UserRole);
    TagProfile *profile = (TagProfile*)data.value<void*>();
    _selectedProfile = profile ;
    bool isEnabled = false;
    if (NULL != _selectedProfile) {
        isEnabled = true;
    }
    ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(isEnabled);
}

void TagChooser::on_treeWidget_itemSelectionChanged()
{
    QList<QTreeWidgetItem *>selItems = ui->treeWidget->selectedItems();
    if (selItems.count() < 1)
        return ;
    setCurrent(selItems.at(0));
}

void TagChooser::on_treeWidget_itemDoubleClicked(QTreeWidgetItem * item, int column)
{
    setCurrent(item);
    QDialog::accept();
}

void TagChooser::on_createProfile_clicked()
{
    TagProfile* profile = _application->getConfigurationManager()->createNewProfile();
    if (NULL == profile) {
        Utility::error(i18n("Unable to create a new profile."));
        return ;
    }
    _selectedProfile = profile ;
    QDialog::accept();
}


void TagChooser::setupData()
{
    _profiles = _application->getConfigurationManager()->getProfilesList();
    if (NULL == _profiles) {
        Utility::message(i18n("Error reading the profile list."));
        return ;
    }
    if (NULL != _profiles) {
        QTreeWidget *pTree = ui->treeWidget;
        int size = _profiles->size();
        for (int i = 0; i < size; i++) {
            TagProfile* profile = _profiles->at(i);
            QTreeWidgetItem *w = new QTreeWidgetItem(pTree);
            w->setText(0, profile->getName());
            w->setText(1, profile->getDescription());
            w->setText(2, profile->getTags().join(","));
            w->setData(0, Qt::UserRole, qVariantFromValue((void*)profile));
            pTree->addTopLevelItem(w);
        }
    }
}

void TagChooser::deleteData()
{
    if (NULL == _profiles) {
        return ;
    }
    int size = _profiles->size();
    for (int i = 0; i < size; i++) {
        TagProfile* profile = _profiles->at(i) ;
        if (profile != _selectedProfile) {
            delete profile ;
        }
    }
    _profiles->clear();
    delete _profiles;
}
